<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
namespace Api\Controller;

use Common\Controller\AppframeController;

header('Content-Type: text/plain; charset=utf-8');
class ShouyeController extends AppframeController
{
 //轮播图
	function lunbotu_lst(){

//		$http = sp_get_host();
		$http = "http://47.104.152.237";
		$data = M("lunbotu")->select();
		 foreach($data as $k=>$v){
			 $data[$k]['imgs'] = $http.$v['img'];
		 }

		if($data){
			exit(json_encode(array('status'=>'200','data'=>$data)));
		}
	}

//首页所有列表
	function shouye_lst(){
			$page = I("page");
			if(empty($page)){
				$page = 1;
			}
			$type = I("type");       //上方标题     1关注  2通告  3推荐  4自荐  5演员  6模特  7童星  8歌手  9舞者  10主播  11新人

		if(!$type){    //默认 type = 3
				exit(json_encode(array('status'=>'10003','data'=>'缺少参数')));
			}

			     if($type == 1){    //关注 列表 没有搜索
				   $uid = I("uid");// 用户的id
//					 $uid = 1;
					 $count =   M("guanzhu g")
					   ->join(" left join " . C("DB_PREFIX") . "account a on g.account_id = a.id")
					   ->where("g.account_id = $uid")
					   ->count();
				   $totalpage = ceil($count/10);

				   $data = M("guanzhu g")
					   ->join(" left join " . C("DB_PREFIX") . "account a on g.account_id = a.id")
					   ->where("g.account_id = $uid")
					   ->field("g.*,a.nickname,a.head_img,a.zhiye_lable,a.address,a.images")
					   ->limit(($page-1)*10,10)
					   ->select();

				   foreach($data as $v){
					   $zhiye[] = $v['zhiye_lable'];
				   }

				   $zhiyes = implode(',',$zhiye);
				   $zhiyess = explode(",",$zhiyes);

				   $zhiye_id = $zhiyess[0];

				   if($zhiye_id){
					   $biaoqian = M("geren_lable")->where("id = $zhiye_id")->field("lable")->find();
				   }
				   $biaoqian_lable = implode(",",$biaoqian) ;    //职业属性的第一个标签
				   foreach($data as $k=>$v){
					   foreach($biaoqian as $kk=>$vv){
						   $data[$k]['lable'] = $biaoqian_lable;
					   }
				   }
//				   exit(json_encode(array('status' => '200', 'data' => array('data'=>$data,'totalpage'=>$totalpage))));
			   }elseif($type==2){     //2通告 列表加搜索

				   $address = I("address");
//					 $address = "北京";
					 $tonggao_biaoqian_id = I("tonggao_biaoqian_id"); //通告的标签
//					 $tonggao_biaoqian_id = 3;
					 $where = " 1 and t.is_del=0";
				   if(!empty($address)){
					   $where .= " and t.address = '{$address}'";
				   }
					if(!empty($tonggao_biaoqian_id)){
						 $where .= " and t.tonggao_biaoqian = '{$tonggao_biaoqian_id}'";
					}

					 //分页
					$count = M("tonggao t")
					 ->join(" left join " . C("DB_PREFIX") . "account a on t.account_id = a.id")
						 ->join(" left join " . C("DB_PREFIX") . "tonggao_lable tl on t.tonggao_biaoqian = tl.id")
						 ->where($where)
						->count();
					 $totalpage = ceil($count/10);

					 $data = M("tonggao t")
						 ->join(" left join " . C("DB_PREFIX") . "account a on t.account_id = a.id")
						 ->join(" left join " . C("DB_PREFIX") . "tonggao_lable tl on t.tonggao_biaoqian = tl.id")
						 ->where($where)
						 ->field("t.*,a.nickname,a.head_img,a.height,tl.lable")
						 ->order("case when t.is_paixu = 0 then 2 else 3 end desc,t.is_paixu asc,t.ctime desc")
						 ->limit(($page-1)*10,10)
						 ->select();

			   }elseif($type==3){      //3推荐 加 搜索

					 $address = I("address");
//		$address = '北京';
					 $zhiye_lable = I("zhiye_lable");//职业属性标签的id

					 $type_lable = I("type_lable");//类型标签的id
//		$type_lable = 48;
					 $techang_lable = I("techang_lable");//特长标签的id

					 $style_lable = I("style_lable");//风格标签的id
					 $appearance_lable = I("appearance_lable");//外貌标签的id
					 $shape_lable = I("shape_lable");//体型标签的id
					 $glamour_lable = I("glamour_lable");//魅力部位标签的id

					 $sex = trim(I("sex"));               //男 女 不限    1男  2女 3保密
		             $old_s = trim(I("old_s"));     //年龄的开始
//		$old_s = 27;
					 $old_e = trim(I("old_e"));     //年龄的结束
//		$old_e = 28;
					 $height_s = trim(I("height_s"));     //身高的开始
					 $height_e = trim(I("height_e"));     //身高的结束

					 $where = " 1 ";
					 if(!empty($address)){
						 $where .= " and a.address = '{$address}'";
					 }
					 if(!empty($zhiye_lable)){
						 $where .= " and  ( FIND_IN_SET($zhiye_lable, a.zhiye_lable)) ";
					 }

					 if(!empty($type_lable)){
						 $where .= " and  ( FIND_IN_SET($type_lable, a.leixing_lable)) ";
					 }
//
					 if(!empty($style_lable)){
						 $where .= " and  ( FIND_IN_SET($style_lable, a.fengge_lable)) ";
					 }
//
					 if(!empty($techang_lable)){
						 $where .= " and  ( FIND_IN_SET($techang_lable, a.techang_lable)) ";
					 }
					 if(!empty($appearance_lable)){
						 $where .= " and  ( FIND_IN_SET($appearance_lable, a.waimao_lable)) ";
					 }
					 if(!empty($shape_lable)){
						 $where .= " and  ( FIND_IN_SET($shape_lable, a.tixing_lable)) ";
					 }
					 if(!empty($glamour_lable)){      //魅力部位
						 $where .= " and  ( FIND_IN_SET($glamour_lable, a.meilibuwei_lable)) ";
					 }

					 if(!empty($sex)){
						 $where .= " and a.sex  = {$sex} ";
					 }
					 if (!empty($old_s)) {
						 $where .= " and {$old_s} <= a.old";
					 }
					 if (!empty($old_e)) {
						 $where .= " and a.old <= {$old_e}";
					 }
					 if (!empty($height_s)) {
						 $where .= " and {$height_s} <= a.height";
					 }
					 if (!empty($height_e)) {
						 $where .= " and a.height <= {$height_e}";
					 }
					 $count =  M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->count();
					 $totalpage = ceil($count/10);


					 $data = M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where. " and a.images != ''")
						  ->field("a.nickname,a.head_img,a.zhiye_lable_id,a.address,a.images,gl.lable,a.id account_id")
						 ->order("case when a.is_paixu = 0 then 2 else 3 end desc,a.is_paixu asc,a.ctime desc")
						 ->limit(($page-1)*10,10)
						 ->select();

			   }elseif($type==4){         //自荐 列表
					 $count =  M("cover c")
						 ->join(" left join " . C("DB_PREFIX") . "account a on c.account_id = a.id")
						 ->join(" left join " . C("DB_PREFIX") . "cover_lable cl on c.cover_lable_id = cl.id")
						 ->where("c.is_del = 0")
						  ->count();
					 $totalpage = ceil($count/10);

					 $data = M("cover c")
						 ->join(" left join " . C("DB_PREFIX") . "account a on c.account_id = a.id")
						 ->join(" left join " . C("DB_PREFIX") . "cover_lable cl on c.cover_lable_id = cl.id")
						 ->field("c.*,a.nickname,a.head_img,cl.lable,a.address")
						 ->where("c.is_del = 0")
						 ->order("c.ctime desc")
						 ->limit(($page-1)*10,10)
						 ->select();

			   }elseif($type==5){       //演员

					 $lable = '演员';
					 $lables = M("geren_lable")->where("lable = '{$lable}'")->field("id")->find();  //演员的id
					 $lables = implode(",",$lables);
					 $where = " 1 ";
					 $where .= " and  ( FIND_IN_SET($lables, a.zhiye_lable)) ";

					 $count =  M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->count();
					 $totalpage = ceil($count/10);


					 $data = M("account a")
						  ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						  ->where($where)
						  ->field("a.address,a.nickname,a.head_img,a.images,a.zhiye_lable_id,gl.lable")
						  ->select();

			   }elseif($type==6){

					 $lable = '模特';
					 $lables = M("geren_lable")->where("lable = '{$lable}'")->field("id")->find();  //演员的id
					 $lables = implode(",",$lables);

					 $where = " 1 ";
					 $where .= " and  ( FIND_IN_SET($lables, a.zhiye_lable)) ";

					 $count =  M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						  ->count();
					 $totalpage = ceil($count/10);

					 $data = M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->field("a.id,a.address,a.nickname,a.head_img,a.images,a.zhiye_lable_id,gl.lable")
						 ->select();
			   }elseif($type==7){
					 $lable = '童星';
					 $lables = M("geren_lable")->where("lable = '{$lable}'")->field("id")->find();  //演员的id
					 $lables = implode(",",$lables);
					 $where = " 1 ";
					 $where .= " and  ( FIND_IN_SET($lables, a.zhiye_lable)) ";

					 $count =  M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						  ->count();
					 $totalpage = ceil($count/10);

					 $data = M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->field("a.id,a.address,a.nickname,a.head_img,a.images,a.zhiye_lable_id,gl.lable")
						 ->select();
			   }elseif($type==8){
					 $lable = '歌手';
					 $lables = M("geren_lable")->where("lable = '{$lable}'")->field("id")->find();  //演员的id
					 $lables = implode(",",$lables);
					 $where = " 1 ";
					 $where .= " and  ( FIND_IN_SET($lables, a.zhiye_lable)) ";

					 $count =  M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						  ->count();
					 $totalpage = ceil($count/10);

					 $data = M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->field("a.id,a.address,a.nickname,a.head_img,a.images,a.zhiye_lable_id,gl.lable")
						 ->select();
			   }elseif($type==9){
					 $lable = '舞者';
					 $lables = M("geren_lable")->where("lable = '{$lable}'")->field("id")->find();  //演员的id
					 $lables = implode(",",$lables);
					 $where = " 1 ";
					 $where .= " and  ( FIND_IN_SET($lables, a.zhiye_lable)) ";

					 $count =  M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						  ->count();
					 $totalpage = ceil($count/10);

					 $data = M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->field("a.id,a.address,a.nickname,a.head_img,a.images,a.zhiye_lable_id,gl.lable")
						 ->select();
			   }elseif($type==10){
					 $lable = '主播';
					 $lables = M("geren_lable")->where("lable = '{$lable}'")->field("id")->find();  //演员的id
					 $lables = implode(",",$lables);
					 $where = " 1 ";
					 $where .= " and  ( FIND_IN_SET($lables, a.zhiye_lable)) ";

					 $count =  M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						  ->count();
					 $totalpage = ceil($count/10);

					 $data = M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->field("a.address,a.nickname,a.head_img,a.images,a.zhiye_lable_id,gl.lable")
						 ->select();
			   }elseif($type==11){
					 $lable = '新人';
					 $lables = M("geren_lable")->where("lable = '{$lable}'")->field("id")->find();  //演员的id
					 $lables = implode(",",$lables);
					 $where = " 1 ";
					 $where .= " and  ( FIND_IN_SET($lables, a.zhiye_lable)) ";

					 $count =  M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->count();
					 $totalpage = ceil($count/10);

					 $data = M("account a")
						 ->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
						 ->where($where)
						 ->field("a.id,a.address,a.nickname,a.head_img,a.images,a.zhiye_lable_id,gl.lable")
						 ->select();
			   }

			exit(json_encode(array('status' => '200', 'data' => array('data'=>$data,'totalpage'=>$totalpage))));
		}


	//首页 通告 点击头像或者昵称进入个人信息页
	function shouye_user(){
		$uid = I("uid");
//		$uid = 17;
		$guanzhu_id = I("guanzhu_id");
//		$guanzhu_id = 13;


		if(!$uid || !$guanzhu_id){
			exit(json_encode(array('status'=>'10003','data'=>'缺少参数')));
		}else{
			$data0 = M("account")->where("id = $guanzhu_id")->find();

			$data1 = M("account")->where("id = $guanzhu_id")
				 ->field("id,name,nickname,head_img,old,phone,sex,birthday,address,school,height,weight,bust,yaowei,hipline,shoe_size,shenfenzheng,album,video,shenfenzheng_photo,is_renzheng,is_qianyue,is_member,is_dongjie,stime,etime,token,images,zhiye_lable_id")
				->find();     //判断  一下相册和视频是否为空
			$biaoqian = "";
			if($data0['zhiye_lable']){
				$biaoqian .= $data0['zhiye_lable'].",";
			}
			if($data0['leixing_lable']){
				$biaoqian .= $data0['leixing_lable'].",";
			}
			if($data0['tixing_lable']){
				$biaoqian .= $data0['tixing_lable'].",";
			}
			if($data0['meilibuwei_lable']){
				$biaoqian .= $data0['meilibuwei_lable'].",";
			}
			if($data0['waimao_lable']){
				$biaoqian .= $data0['waimao_lable'].",";
			}
			if($data0['techang_lable']){
				$biaoqian .= $data0['techang_lable'].",";
			}
			if($data0['fengge_lable']){
				$biaoqian .= $data0['fengge_lable'].",";
			}


			 if(empty($biaoqian)){
				 $biaoqians = '0';
			 }else{
				 $biaoqians = substr($biaoqian,0,strlen($biaoqian)-1);
			 }
//			var_dump($biaoqians) ;die;

				 $data2 = M("cover c")
				->join(" left join " . C("DB_PREFIX") . "account a on c.account_id = a.id")
				->join(" left join " . C("DB_PREFIX") . "cover_lable cl on c.cover_lable_id = cl.id")
				->where("c.is_del = 0")
				->field("c.*,a.nickname,a.head_img,cl.lable,a.address")
				->order("c.ctime desc")
				->select();
			$data3 = M("guanzhu")->where("account_id = $uid and guanzhu_id = $guanzhu_id")->find();

			if($data3){
				$guanzhu = '1';
			}else{
				$guanzhu = '0';
			}
//			       var_dump($guanzhu) ;die;
			exit(json_encode(array('status'=>'200','data'=>array('data1'=>$data1,'data2'=>$data2,'guanzhu_shifou'=>$guanzhu,'biaoqian'=>$biaoqians))));

		}
	}


	//点击关注
	function shouye_gerenye_guanzhu(){
		$id = I("uid");  //用户id
//		$id = 2;
		$acc = I("guanzhu_id");//被关注的id
//		$acc = 1;
		if(!$id || !$acc){
			exit(json_encode(array('status'=>'10003','data'=>'缺少参数')));
		}
		$dat['account_id'] = $id;
		$dat['guanzhu_id'] = $acc;
		$dat['ctime'] = time();
		M("guanzhu")->add($dat);
		exit(json_encode(array('status'=>'200','data'=>'关注成功')));
	}
	//点击取消关注
	function shouye_gerenye_quxiao_guanzhu(){
		$id = I("uid");  //用户id
//		$id = 2;
		$acc = I("guanzhu_id");//被关注的id
//		$acc = 1;
		if(!$id || !$acc){
			exit(json_encode(array('status'=>'10003','data'=>'缺少参数')));
		}
		$dat['account_id'] = $id;
		$dat['guanzhu_id'] = $acc;
		$dat['ctime'] = time();
		$data = M("guanzhu")->where("account_id = $id and guanzhu_id = $acc")->delete();
		if($data){
			exit(json_encode(array('status'=>'200','data'=>'取消关注成功')));
		}
	}

	 //发布邀约
	 function fabu_yaoyue(){
		 $uid = I("account_id");  //用户的id
//		 $uid = 1;
		 $yaoyue_id = I("yaoyue_id");  //邀约人的id
//		 $yaoyue_id = 2;
		 if(!$uid || !$yaoyue_id){
			 exit(json_encode(array('status'=>'10003','data'=>'缺少参数')));
		 }
		 $content =  I("content");
		  $dat['content'] =  $content;
//		 $dat['content'] = "nihao a";
		  $dat['ctime'] = time();
		  $dat['account_id']  = $uid;    //被邀约人id
		  $dat['yaoyueren_id']  = $yaoyue_id;    //要约人id
		  M("yaoyue")->add($dat);


		 //登录  邀约消息的发送 开始
		 include "./rongyun/master/API/rongcloud.php";
		 $appKey = 'kj7swf8okivm2';
		 $appSecret = 'TAEhDLABQsMjf';
		 $RongCloud = new \RongCloud($appKey,$appSecret);
		 // 发送单聊消息方法（一个用户向另外一个用户发送消息，单条消息最大 128k。每分钟最多发送 6000 条信息，每次发送用户上限为 1000 人，如：一次发送 1000 人时，示为 1000 条消息。）
		 $fromUserId = "2";    //发送人用户 Id
		 $toUserId = $yaoyue_id; //接收用户 Id，可以实现向多人发送消息，每次上限为 1000 人
	     $RongCloud->message()->publishPrivate($fromUserId, $toUserId, 'RC:TxtMsg',"{\"content\":\"{$content}\",\"extra\":\"\",\"duration\":20}", 'thisisapush', '{\"pushData\":\"\"}', '4', '0', '0', '0', '0');

		 exit(json_encode(array('status'=>'200','data'=>'邀约成功')));
	 }

	   //邀约列表
	function bei_yaoyue(){
		$page = I("page");
		if(empty($page)){
			$page = 1;
		}
		$beiyaoyue_id = I("beiyaoyue");
		if(!$beiyaoyue_id){
			exit(json_encode(array('status'=>'10003','data'=>'缺少参数')));
		}
		$count =   M("yaoyue")->where("yaoyueren_id = $beiyaoyue_id")->count();
		$totalpage = ceil($count/10);

		$data = M("yaoyue")->where("yaoyueren_id = $beiyaoyue_id")
			->field("id,account_id,yaoyueren_id,content,ctime")
			->order("ctime desc")
			->limit(($page-1)*10,10)
			->select();
		exit(json_encode(array('status' => '200', 'data' => array('data'=>$data,'totalpage'=>$totalpage))));
	}


	//举报 提交
	function jubao_sav(){
		$uid = I("uid");    //举报人的id
//	$uid = 1;
		$bei_jubao = I("beijubao");   // 被举报人的id
//		$bei_jubao = 13;
		$content = trim(I("content"));     //举报的内容
//		$content = "长期以来，广大网民积极举报网上违法和不良信息，在净化网络生态、营造清朗网络空间方面发挥了重要作用，在此谨致诚挚感谢。";
		if(!$uid || !$content || !$bei_jubao){
			exit(json_encode(array('status'=>'10003','data'=>'缺少参数')));
		}else{
			$dat['account_id'] = $uid;
			$dat['beijubao_id'] = $bei_jubao;
			$dat['content'] = $content;
			$dat['ctime'] = time();
			M("report")->add($dat);
			exit(json_encode(array('status'=>'200','data'=>"举报保存成功")));
		}
	}
	//首页 右上角搜索
	function shouye_right_sousuo(){
		$page = I("page");
		if(empty($page)){
			$page = 1;
		}
		$name = trim(I("name"));   //有可能是用户的昵称  有可能是通告的名称
		$where1 = " 1 and t.is_del = 0";
		$where2 = " 1 ";
		if(!empty($name)){
			$where1 .= " and t.name like '%{$name}%'";
			$where2 .= " and a.nickname like '%{$name}%'";
		}

		$data1 = M("tonggao t")      //通告
		->join(" left join " . C("DB_PREFIX") . "account a on t.account_id = a.id")
			->join(" left join " . C("DB_PREFIX") . "tonggao_lable tl on (t.yingshibiaoyan_lable_id = tl.id or t.wangluoxuanchuan_lable_id = tl.id or t.huizhanhuodong_lable_id = tl.id or t.pingmianpaishe_lable_id = tl.id)")
			->where($where1)
			->field("t.*,a.nickname,a.head_img,tl.lable,a.height")
			->order("t.ctime desc")
			->limit(($page-1)*10,10)
			->select();
		$data2 = M("account a")        //推荐
			->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
			->where($where2)
			->field("a.nickname,a.head_img,gl.lable,a.address,a.images")
			->order("a.ctime desc")
			->limit(($page-1)*10,10)
			->select();
		if($data1){
			$count =   M("tonggao t")      //通告
			->join(" left join " . C("DB_PREFIX") . "account a on t.account_id = a.id")
				->join(" left join " . C("DB_PREFIX") . "tonggao_lable tl on (t.yingshibiaoyan_lable_id = tl.id or t.wangluoxuanchuan_lable_id = tl.id or t.huizhanhuodong_lable_id = tl.id or t.pingmianpaishe_lable_id = tl.id)")
				->where($where1)
				->count();
			$totalpage = ceil($count/10);
		}elseif($data2){
			$count =  M("account a")        //推荐
			->join(" left join " . C("DB_PREFIX") . "geren_lable gl on a.zhiye_lable_id = gl.id")
				->where($where2)
				->count();
			$totalpage = ceil($count/10);
		}

		exit(json_encode(array('status'=>'200','data'=>array('data1'=>$data1,'data2'=>$data2,'totalpage'=>$totalpage))));
	}






























}